package org.itheima.googleplay.ui.fragment;

import java.util.List;
import java.util.Random;

import org.itheima.googleplay.http.RecommendProtocol;
import org.itheima.googleplay.ui.fragment.LoadingPager.LoadedResult;
import org.itheima.googleplay.ui.weiget.StellarMap;
import org.itheima.googleplay.ui.weiget.StellarMap.Adapter;
import org.itheima.googleplay.utils.UIUtils;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

/**
 * @包名: org.itheima.googleplay.ui.fragment
 * @类名: RecommendFragment
 * @作者: 肖琦
 * @创建时间 : 2015-3-23 下午2:37:12
 * 
 * @描述: TODO
 * 
 * @SVN版本: $Rev: 18 $
 * @更新人: $Author: xq $
 * @更新时间: $Date: 2015-03-23 16:00:13 +0800 (星期一, 23 三月 2015) $
 * 
 * @更新描述:
 */
public class RecommendFragment extends BaseFragment
{
	private RecommendProtocol	mProtocol;
	private List<String>		mDatas;

	@Override
	protected View onSuccessView()
	{
		// 新建View
		// 用来管理 随机摆放 的View的
		StellarMap stellarMapView = new StellarMap(UIUtils.getContext());

		// 给数据给到StellarMap
		stellarMapView.setAdapter(new RecommendAdapter());

		// 设置内边距
		stellarMapView.setInnerPadding(UIUtils.dip2px(10), UIUtils.dip2px(10), UIUtils.dip2px(10), UIUtils.dip2px(10));

		// 设置区域划分
		stellarMapView.setRegularity(20, 30);

		// 设置默认选中页
		stellarMapView.setGroup(0, true);

		return stellarMapView;
	}

	@Override
	protected LoadedResult onLoadData()
	{
		// 新建协议
		mProtocol = new RecommendProtocol();

		// 去网络获取数据
		try
		{
			mDatas = mProtocol.loadData(0);

			return checkState(mDatas);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return LoadedResult.ERROR;
		}
	}

	class RecommendAdapter implements Adapter
	{
		private int	PAGE_SIZE	= 15;

		// 数据可以显示多少个页面
		@Override
		public int getGroupCount()
		{
			if (mDatas != null)
			{
				int size = mDatas.size();
				int count = size / PAGE_SIZE;

				if (size % PAGE_SIZE > 0)
				{
					count++;
				}
				return count;
			}
			return 0;
		}

		// 第Group个页面有几个数据
		@Override
		public int getCount(int group)
		{
			if (mDatas != null)
			{
				int size = mDatas.size();
				int groupCount = getGroupCount();

				if (groupCount != group)
				{
					// 不是最后一页
					return PAGE_SIZE;
				}
				else
				{
					if (size % PAGE_SIZE == 0)
					{
						return PAGE_SIZE;
					}
					else
					{
						// 最后一页
						return size % PAGE_SIZE;
					}
				}
			}
			return 0;
		}

		@Override
		public View getView(int group, int position, View convertView)
		{
			// group：当前是第几页
			// position: 当前页面的第几个
			int index = position + group * PAGE_SIZE;
			String text = mDatas.get(index);

			TextView tv = new TextView(UIUtils.getContext());

			// 给textView设置属性
			tv.setText(text);// 设置文本

			Random rdm = new Random();
			// 设置随机色
			int red = rdm.nextInt(210) + 20;
			int green = rdm.nextInt(210) + 20;
			int blue = rdm.nextInt(210) + 20;
			int color = Color.argb(0xff, red, green, blue);
			tv.setTextColor(color);// 设置文本颜色

			// 设置随机的大小 14 ---> 24 (24 - 14) + 基本值 + 14
			int size = rdm.nextInt(11) + 14;
			tv.setTextSize(UIUtils.dip2px(size));// 设置文本字体大小

			return tv;
		}

		@Override
		public int getNextGroupOnPan(int group, float degree)
		{
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public int getNextGroupOnZoom(int group, boolean isZoomIn)
		{
			// TODO Auto-generated method stub
			return 0;
		}

	}

}
